package com.lk.api.app;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lk.api.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lk on 2014/12/16.
 */
public class MyMenu extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.hello_world);
        registerForContextMenu(getListView());
        ArrayAdapter<String>  data = new ArrayAdapter<String>(MyMenu.this, android.R.layout.simple_list_item_1, data());
        setListAdapter(data);
    }

    public List<String> data() {
        List<String> addTime = new ArrayList<String>();
//        for (int i = 0; i < 5; i++){
//            addTime.add("" + i);
//        }
        addTime.add("alertDialogIcon");
        return addTime;
    }

    public List<String> dataAlertDialog() {
        List<String> addTime = new ArrayList<String>();
        for (int i = 0; i < 5; i++){
            addTime.add("" + i);
        }
        return addTime;
    }

    //打开activity的时候 创建
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.menu_api, menu);
        Log.d("onCreateOptionsMenu", "onCreateOptionsMenu");
        return super.onCreateOptionsMenu(menu);
    }

    //打开activity的时候 调用
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.d("onPrepareOptionsMenu", "onPrepareOptionsMenu");
        return super.onPrepareOptionsMenu(menu);
    }

    /*
      * 长按
      * */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

//        menu.add(1,1,1,"menu_save");
//        menu.add(1,2,2,"menu_del");
//        menu.add(1,3,3,"menu_upd");
//        menu.add(1,4,4,"menu_sel");
        getMenuInflater().inflate(R.menu.menu_api, menu);
        Log.d("onCreateContextMenu", "" + v.getId());
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /*
    * 长按 弹出菜单 并且点击调用
    * */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Log.d("getTitle", "" + item.getTitle());
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Log.d("onContextItemSelected:", "" + info.position);
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "" + position, Toast.LENGTH_LONG).show();
        Log.d("onListItemClick:", "" + ((TextView) v).getText());
        super.onListItemClick(l, v, position, id);
        if (position == 0) {
            ArrayAdapter<String>  data = new ArrayAdapter<String>(MyMenu.this, android.R.layout.simple_list_item_1, dataAlertDialog());
            AlertDialog alertDialog = new AlertDialog.Builder(MyMenu.this)
                    .setTitle("Hello World")
                    .setIcon(android.R.drawable.ic_dialog_alert)
//                    .setMessage("我是正文")
                    .setAdapter(data,new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MyMenu.this,""+which,Toast.LENGTH_LONG).show();

                        }
                    })
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();


                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
            alertDialog.show();
        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//
//            return true;
//        }
        Log.d("onOptionsItemSelected：", "" + id);
        return super.onOptionsItemSelected(item);
    }
}
